package com.cyys.modules.system.model.org;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.SqlCondition;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cyys.common.utils.LoggerUtil;
import com.cyys.common.utils.StringUtil;
import com.cyys.common.validate.Edit;
import com.cyys.common.validate.Insert;
import com.cyys.modules.core.model.TreeBean;
import com.cyys.modules.core.sysParam.OrgUtil;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import com.cyys.modules.core.sysParam.ParamRegionUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import javax.validation.constraints.NotBlank;

/**
 * User generated by hbm2java
 */
@TableName("sys_org")
@Data
public class Org extends TreeBean {

	@TableField(condition = SqlCondition.LIKE)
	@NotBlank(message = "机构名称不能为空",groups = {Edit.class, Insert.class})
	private String name;   //单位名称

	@TableField(updateStrategy=FieldStrategy.NOT_NULL)
	private String province;  //省

	@TableField(updateStrategy=FieldStrategy.NOT_NULL)
	private String city;  //市

	@TableField(updateStrategy=FieldStrategy.NOT_NULL)
	private String county;  //县

	private String regionCode; //地域编码
	
	private String level;  //级别

	private String type;  //机构类型

//	@TableField(exist = false)
//	private String orgChilds;  //所有子机构id，以，号隔开

	@JsonIgnore
	public String getParentName(){
		if(StringUtil.isNotBlank(getParentId())){
			if("0".equals(getParentId())){
				return "顶级节点";
			}
			try {
				Org org= OrgUtil.get(getParentId());
				if(org!=null){ //此判断以防删除机构或者其他意外
					return org.getName();
				}else{
					return "";
				}
			}catch (Exception e){
				LoggerUtil.error(Org.class,"机构缓存不能获取",e);
				return "";
			}
		}
		return "";
	}

	@JsonIgnore
	public String getProvinceName() {
		if(StringUtil.isNotBlank(province) && ParamRegionUtil.getParamRegionCodeMap(province) != null){
			return ParamRegionUtil.getParamRegionCodeMap(province).getName();
		}
		else
			return "";
	}

	@JsonIgnore
	public String getCountyName() {
		if(StringUtil.isNotBlank(county)  && ParamRegionUtil.getParamRegionCodeMap(county) != null)
			return ParamRegionUtil.getParamRegionCodeMap(county).getName();
		else
			return "";
	}

	@JsonIgnore
	public String getCityName() {
		if(StringUtil.isNotBlank(city)  && ParamRegionUtil.getParamRegionCodeMap(city) != null)
			return ParamRegionUtil.getParamRegionCodeMap(city).getName();
		else
			return "";
	}

	@JsonIgnore
	public String getTypeName() {
		if(StringUtil.isNotBlank(type))
			return ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.ORG_TYPE).get(type);
		else
			return "";
	}
}
